package menuaction.jasperTools;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import exception.JasperParamsNotFoundException;

/**
 * klasa zaczyta z xmla parametry raportu
 * @author looser
 *
 */
public class JasperParamsAdapter 
{
	private static final String XML_TAG_JASPER_PARAMS= "jasperParams";
	private static final String XML_TAG_PARAMS= "params";
	private static final String XML_TAG_PARAM= "param";
	private static final String XML_TAG_REC_ED_DIR= "recordEditDirectory";
	private static final String XML_TAG_REC_ED_NAME= "recordEditName";
	private static final String XML_TAG_JASPER_DIR= "jasperFileDirectory";
	
	private static final String XML_PARAM_ATTRIB_NAME= "name";
	private static final String XML_PARAM_ATTRIB_TYPE= "type";
	private static final String XML_PARAM_ATTRIB_DIR= "dir";
	
	private static final String XML_PARAM_TYPE_STRING= "String";
	private static final String XML_PARAM_TYPE_INT= "Int";
	private static final String XML_PARAM_TYPE_DOUBLE= "Double";
	
	private String xmlFile;
	private String paramName;
	private JasperRunnerParams jasperParams;
	
	public JasperParamsAdapter(String xmlFile,String paramName)
	{
		this.xmlFile = xmlFile;
		this.paramName = paramName;		
	}
	public JasperRunnerParams getParams() throws SAXException, IOException, JasperParamsNotFoundException
	{
		DOMParser xmlparser = new DOMParser(); 
		xmlparser.parse(xmlFile);
		Document doc = xmlparser.getDocument(); 
		
		NodeList params=doc.getElementsByTagName(JasperParamsAdapter.XML_TAG_JASPER_PARAMS);
		int i=0;
		for(;i<params.getLength();++i)
		{
			if(params.item(i)instanceof Element)
			{
				Element elem = (Element) params.item(i);
				if(elem.getAttribute(JasperParamsAdapter.XML_PARAM_ATTRIB_NAME).equals(paramName))
				{
					jasperParams = loadParams(elem);
					return jasperParams;
				}
			}
		}
		throw new JasperParamsNotFoundException("W pliku '"+xmlFile+"' nie istnieje zestaw parametrow o nazwie '"+paramName+"'");
	}
	private JasperRunnerParams loadParams(Element elem) 
	{
		String jrxmlFile=((Element) elem.getElementsByTagName(JasperParamsAdapter.XML_TAG_JASPER_DIR).item(0)).getAttribute(JasperParamsAdapter.XML_PARAM_ATTRIB_DIR);
		String xmlFile=((Element) elem.getElementsByTagName(JasperParamsAdapter.XML_TAG_REC_ED_DIR).item(0)).getAttribute(JasperParamsAdapter.XML_PARAM_ATTRIB_DIR);
		String recEdName=((Element) elem.getElementsByTagName(JasperParamsAdapter.XML_TAG_REC_ED_NAME).item(0)).getAttribute(JasperParamsAdapter.XML_PARAM_ATTRIB_NAME);
		
		Map<String,Object> params = new Hashtable<String, Object>();
		NodeList jparams = elem.getElementsByTagName(JasperParamsAdapter.XML_TAG_PARAMS);
		jparams = ((Element)jparams.item(0)).getElementsByTagName(JasperParamsAdapter.XML_TAG_PARAM);
		for(int i=0;i<jparams.getLength();++i)
		{
			if(jparams.item(i) instanceof Element)
			{
				Element ep=(Element)jparams.item(i);
				String name=ep.getAttribute(JasperParamsAdapter.XML_PARAM_ATTRIB_NAME);
				String type=ep.getAttribute(JasperParamsAdapter.XML_PARAM_ATTRIB_TYPE);
				
				if(type.equals(JasperParamsAdapter.XML_PARAM_TYPE_DOUBLE))
				{
					params.put(name, new Double(0));
				}
				else if(type.equals(JasperParamsAdapter.XML_PARAM_TYPE_INT))
				{
					params.put(name, new Integer(0));
				}
				else if(type.equals(JasperParamsAdapter.XML_PARAM_TYPE_STRING))
				{
					params.put(name, new String());
				}
									
			}
		}
		return new JasperRunnerParams(jrxmlFile,xmlFile,recEdName,params,paramName);
	}
}
